def prost(num):
    for i in range(2, int(num**0.5) +1):
        if num % i == 0:
            return False
    return True

def max_prost(num):
    if num < 1000 or num > 9999:
        return 0

    q = num // 1000
    w = (num % 1000) // 100
    e = (num % 100) // 10
    r = num % 10
    maxim = 0
    for cur in (r, e, w, q, q*10 +w, w*10 +e,e*10 +r,q*100 +w*10+e, w*100 +e*10+r,num):
        if prost(cur) and cur>maxim:
            maxim= cur
    return maxim

num = int(input())
res = max_prost(num)
print(res)